package com.tekj.cxqcb.view.Module.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.squareup.picasso.Picasso;
import com.suke.widget.SwitchButton;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.MemberUpgradePayBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.plugins.pay.GetAlipayParameter;
import common.plugins.pay.GetWeixinParameter;
import common.tool.Tools;
import common.widget.CircleTransform;
import es.dmoral.toasty.Toasty;

/**
 * 身份升级Activity
 */
public class IdentityActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.img_sdv_head)
    SimpleDraweeView imgSdvHead;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_identity)
    TextView tvIdentity;
    @BindView(R.id.ll_user_information)
    LinearLayout llUserInformation;
    @BindView(R.id.bnt_upgrade)
    TextView bntUpgrade;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.rb_weixin)
    RadioButton rbWeixin;
    @BindView(R.id.ll_weixin)
    LinearLayout llWeixin;
    @BindView(R.id.rb_alipay)
    RadioButton rbAlipay;
    @BindView(R.id.ll_alipay)
    LinearLayout llAlipay;
    @BindView(R.id.ll_yhk)
    LinearLayout llYhk;
    @BindView(R.id.sb_Yue)
    SwitchButton sbYue;
    @BindView(R.id.ll_yue)
    LinearLayout llYue;
    @BindView(R.id.btn_click)
    TextView btnClick;
    @BindView(R.id.tv_upVoucher)
    TextView tvUpVoucher;
    @BindView(R.id.tv_yhk_name)
    TextView tvYhkName;
    @BindView(R.id.tv_yuetext)
    TextView tvYuetext;
    @BindView(R.id.rb_yhk)
    RadioButton rbYhk;
    @BindView(R.id.ll_body)
    LinearLayout llBody;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    @BindView(R.id.tv_look)
    TextView tvLook;
    @BindView(R.id.tv_isfk)
    TextView tvIsfk;
    @BindView(R.id.tv_endtime)
    TextView tvEndtime;
    int paytype;

    RadioButton radioButtons[];
    BusinessDao businessDao;

    @Override
    public void init() {
        businessDao = new BusinessDao(activity, this);
        tvTitle.setText("商业伙伴");
        tvR.setText("记录");
        tvR.setTextColor(getResources().getColor(R.color.colorPrimary));

//        String text = "成为本区域的运管中心，您将服务于您的下属的城市合伙人，并因此获得本区域城市合伙人售车成功后"
//                + "<span style='color:#74a7ff'>" + "700元/辆 " + "<span/>"
//                + "的服务费和本区域内会员现金消费"
//                + "<span style='color:#74a7ff'>" + "0.5% " + "<span/>"
//                + "的奖励。";
//
//        tvContent.setText(Html.fromHtml(text));
        radioButtons = new RadioButton[]{rbAlipay, rbWeixin, rbYhk};

        setViewData();

        setPayType(0, false);
        sbYue.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                setPayType(0, isChecked);
            }
        });

    }

    @Override
    public int setLayout() {
        return R.layout.activity_identity;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {

        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 支付宝支付:
                MemberUpgradePayBean memberUpgradePayBean = (MemberUpgradePayBean) bindingViewBean.getBean();
                if (memberUpgradePayBean.getCode().equals("0000000")) {
                    if (memberUpgradePayBean.getData().getPay() == 0) {
                        activity.finish();
                        Toasty.normal(activity, memberUpgradePayBean.getMsg()).show();
                        return;
                    }
                    try {
                        new GetAlipayParameter(this, memberUpgradePayBean.getData().getAlipay());
                    } catch (Exception e) {
                        Toasty.normal(activity, "支付出错").show();
                    }
                }
                break;

            case 微信支付:
                MemberUpgradePayBean memberUpgradePayBean2 = (MemberUpgradePayBean) bindingViewBean.getBean();
                if (memberUpgradePayBean2.getCode().equals("0000000")) {
                    if (memberUpgradePayBean2.getData().getPay() == 0) {
                        activity.finish();
                        Toasty.normal(activity, memberUpgradePayBean2.getMsg()).show();
                        return;
                    }
                    new GetWeixinParameter(this, memberUpgradePayBean2.getData().getWxpay());
                }
                break;

            case 加盟费支付:

                break;
        }

    }

    @OnClick({R.id.ll_weixin, R.id.ll_alipay, R.id.ll_yhk, R.id.btn_click,
            R.id.tv_upVoucher, R.id.ll_back, R.id.ll_right, R.id.bnt_upgrade})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_weixin:
                setPayType(1, sbYue.isChecked());
                break;
            case R.id.ll_alipay:
                setPayType(0, sbYue.isChecked());
                break;
            case R.id.ll_yhk:
                setPayType(2, sbYue.isChecked());
                break;
            case R.id.btn_click:
                Tools.showNormalDialogOne(activity, "支付", "是否支付？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        businessDao.AllianceBusiness(MainBActivity.User.getUserId(), paytype);
                    }
                }, null);

                break;
            case R.id.tv_upVoucher:

                break;
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_right:
                startActivity(new Intent(activity, UpdataListActivity.class));
                break;
            case R.id.bnt_upgrade:

                break;
        }
    }

    void setPayType(int type, boolean isBalance) {
        for (int i = 0; i < radioButtons.length; i++) {
            radioButtons[i].setChecked(i == type);
        }

        if (type == 0) {
            paytype = 1;
            if (isBalance) {
                paytype = 5;
            }
        } else if (type == 1) {
            paytype = 2;
            if (isBalance) {
                paytype = 6;
            }
        } else if (type == 2) {
            paytype = 3;
            if (isBalance) {
                paytype = 7;
            }
        }
    }

    void setViewData() {

        tvEndtime.setText(" 将于" + MainBActivity.User.getEndTime() + "到期");
        tvUsername.setText(MainBActivity.User.getName());
        Picasso.get()
                .load(MainBActivity.User.getHead().equals("") ? "1" : MainBActivity.User.getHead())
                .transform(new CircleTransform())
                .into(imgSdvHead);
        tvIdentity.setText("代理区域：" + MainBActivity.User.getAddress());
        if (MainBActivity.User.getRole().equals("7")) {
//            4城市合伙人5运管中心7省分公司
            tvLook.setText("省分公司");
        } else if (MainBActivity.User.getRole().equals("5")) {
            tvLook.setText("运管中心");
        } else {
            tvLook.setText("城市合伙人");
        }

        if (MainBActivity.User.getPaystate() == 3) {
            tvIsfk.setText("（已缴纳审核通过）");
            scrollView.setVisibility(View.GONE);
            llBody.setVisibility(View.VISIBLE);
        } else if (MainBActivity.User.getPaystate() == 2) {
            tvIsfk.setText("（已支付待审核）");
            scrollView.setVisibility(View.GONE);
            llBody.setVisibility(View.VISIBLE);
        } else if (MainBActivity.User.getPaystate() == 1) {
            tvIsfk.setText("（未付款）");
            scrollView.setVisibility(View.VISIBLE);
            llBody.setVisibility(View.GONE);
        } else {
            tvIsfk.setText("");
            scrollView.setVisibility(View.GONE);
            llBody.setVisibility(View.VISIBLE);
        }
    }

}
